<template>
  <div class="trainerBox">
    <el-form
      @submit.native.prevent
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="70px"
    >
      <el-form-item label="抽查名称" prop="title">
        <!-- 输入框 -->
        <el-input
          v-model="queryParams.title"
          placeholder="请输入抽查名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
        <el-button
          v-preventReClick
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      height="330"
      border
      v-loading="loading"
      :data="postList"
      ref="tableRef"
    >
      <el-table-column
        show-overflow-tooltip
        label="抽查名称"
        align="center"
        prop="title"
      />

      <el-table-column
        show-overflow-tooltip
        label="下发人数"
        align="center"
        prop="sendNum"
      />
      <el-table-column
        show-overflow-tooltip
        label="未及格人数"
        align="center"
        prop="failNum"
      />
      <el-table-column
        show-overflow-tooltip
        label="未完成人数"
        align="center"
        prop="unFinishNum"
      />
      <el-table-column
        show-overflow-tooltip
        label="完成率"
        align="center"
        prop="finishRate"
      >
        <template slot-scope="scope"> {{ scope.row.finishRate }}% </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="及格率"
        align="center"
        prop="passRate"
      >
        <template slot-scope="scope"> {{ scope.row.passRate }}% </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="创建时间"
        align="center"
        prop="createTime"
      >
        <!-- 格式化时间 -->
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { spotCheckList } from "@/api/screen/spotCheck";
export default {
  props: {
    active: [String, Number],
  },
  data() {
    return {
      loading: false,
      postList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,

        title: undefined,
      },
      total: 10,
    };
  },
  mounted() {},
  watch: {
    active: {
      handler(newVal) {
        if (newVal == 0) {
          this.getList();
        }
      },
      immediate: true,
    },
  },
  methods: {
    async getList() {
      let res;
      try {
        this.loading = true;

        res = await spotCheckList(this.queryParams);
        if (res.code == 200) {
          this.postList = res.rows;
          this.total = res.total;
          // 页面数据更新后，让表格滚动条回到顶部
          this.$nextTick(() => {
            if (this.$refs.tableRef) {
              this.$refs.tableRef.bodyWrapper.scrollTop = 0;
            }
          });
        }
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/screen/spotCheck/spotCheckExport",
        {
          ...this.queryParams,
        },
        `spotCheckExport_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
</style>